package com.ldatum.crm.action.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.domain.TLogs;
import com.ldatum.crm.domain.TSchedule;
import com.ldatum.crm.domain.ToDoItem;
import com.ldatum.crm.service.LogsService;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.service.TScheduleService;
import com.ldatum.crm.service.ToDoItemService;
import com.ldatum.crm.serviceImpl.LogsServiceImpl;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;
import com.ldatum.crm.serviceImpl.TScheduleServiceImpl;
import com.ldatum.crm.serviceImpl.ToDoItemServiceImpl;
import com.ldatum.crm.util.MD5;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

@ParentPackage("admin")
@Results({ 
	@Result(name = "mainAction", location = "admin!main.action",type = "redirect")
})
public class AdminAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private List<TEmployee> employeeList = new ArrayList<TEmployee>();
	private List<ToDoItem> toDoItemList = new ArrayList<ToDoItem>();
	private List<TSchedule> scheduleList = new ArrayList<TSchedule>();
	
	private TEmployeeService employeeService = new TEmployeeServiceImpl();
	private LogsService logsService = new LogsServiceImpl();
	private ToDoItemService toDoItemService = new ToDoItemServiceImpl();
	private TScheduleService scheduleService = new TScheduleServiceImpl();
	
	private TEmployee employee = new TEmployee();
	private ToDoItem toDoItem = new ToDoItem();
	
	private String alertMessage;//警告消息
	private String loginname;//登录账号
	private String password;//密码
	
	private String levelOne;
	private String levelTwo;
	
	/**
	 * 
	 * 描述 : 返回登录界面
	 * @return
	 */
	public String login(){
		return "index";
	}
	
	/**
	 * 
	 * 描述 : 实现登录，用户名密码正确则跳转到主界面，否则提示并返回返回到登录界面. <br>
	 * @return
	 * @throws Exception
	 */
	@InputConfig(resultName = "index")
	public String doLogin() throws Exception{
			TEmployee emp ;
			if("".equals(loginname) | "".equals(password)){
				alertMessage = "登录名和密码不能为空";
				return "index";
			}
			try{
				emp = employeeService.findByLoginname(TEmployee.class,loginname);
			}catch(Exception e){
				e.printStackTrace();
				alertMessage = "程序异常,请联系开发人员";
				return "index";
			}
			
			if (emp == null){
				alertMessage = "登录名不存在";
				return "index";
			}			
			if("离职".equals(emp.getStatus())){
				alertMessage = "此员工是离职状态，不能登录!";
				return "index";
			}
			if (MD5.md5(password).equals(emp.getPassword())){
				toDoItemList = toDoItemService.findUnFinished(emp.getFullname());
				scheduleList = scheduleService.findUnFinished(emp.getFullname());
				setSession(Admin.DAIBANSHIXIANG_COUNTS,toDoItemList.size());
				setSession("toDoItemList",toDoItemList);
				setSession(Admin.FUWUJIHUA_COUNTS,scheduleList.size());
				setSession("scheduleList",scheduleList);
				
				setSession(Admin.LOGIN_EMPLOYEE_ID_SESSION_NAME,emp.getId());
				setSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME,emp.getFullname());
				setSession(Admin.LOGIN_EMPLOYEE_PIC_ADDRESS,emp.getHeadPicture());
				setSession("authorityValue",emp.getPosition().getAuthorityValue());
				/**
				 * 保存登陆日志
				 */
				TLogs logs = new TLogs();
				logs.setType("员工登陆");
				logs.setEventdetail(emp.getFullname()+"登陆了系统");
				logs.setOperatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()));
				logs.setOperator(emp.getFullname());
				logsService.save(logs);
				return "mainAction";
			}else{
				alertMessage = "密码错误";
				return "index";
			}
	}
	/**
	 * 主界面
	 * @return
	 * @throws Exception
	 */
	@InputConfig(resultName = "main")
	public String main() throws Exception {
		employee = employeeService.findById(TEmployee.class,3);
		levelOne = "index";
		levelTwo = "";
		return "main";
	}
	
	public String logout() {
		getRequest().getSession().removeAttribute(Admin.LOGIN_EMPLOYEE_ID);
		return "index";
	}
	
	/**
	 * 描述 : 用户密码忘记了,向注册邮箱发送邮件. <br>
	 * @return
	 */
	public void forgetPasswd(){
		
	}
	
	/**
	 * 描述 : 用户从主界面回到登录界面，并删除用户session. <br>
	 * @return
	 */
	public String  lock(){
		getRequest().getSession().removeAttribute(Admin.LOGIN_EMPLOYEE_ID);
		return "lock";
	}
	
	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}

	public TEmployee getEmployee() {
		return employee;
	}

	public void setEmployee(TEmployee employee) {
		this.employee = employee;
	}

	public String getAlertMessage() {
		return alertMessage;
	}

	public void setAlertMessage(String alertMessage) {
		this.alertMessage = alertMessage;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}

	public List<ToDoItem> getToDoItemList() {
		return toDoItemList;
	}

	public void setToDoItemList(List<ToDoItem> toDoItemList) {
		this.toDoItemList = toDoItemList;
	}

	public ToDoItem getToDoItem() {
		return toDoItem;
	}

	public void setToDoItem(ToDoItem toDoItem) {
		this.toDoItem = toDoItem;
	}

	public List<TSchedule> getScheduleList() {
		return scheduleList;
	}

	public void setScheduleList(List<TSchedule> scheduleList) {
		this.scheduleList = scheduleList;
	}
	
}
